package com.dragon.client.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.IVersionService;
import com.dragon.repository.domain.dto.VersionDTO;
import com.dragon.repository.domain.vo.VersionVO;
import com.dragon.repository.entity.Version;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 版本信息 前端控制器
 * </p>
 *
 * @author sy
 * @since 2024-01-17
 */
@RestController
@RequestMapping("version")
@Api(tags = "版本信息")
public class VersionController {

    @Autowired
    IVersionService service;

    @GetMapping("list")
    @ApiOperation(value = "版本列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int")
    })
    public ResponseData<List<VersionDTO>> versionList(
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Version> versionPage = service.versionList(page, limit);
        return ResponseData.success(VersionDTO.convertList(versionPage.getRecords()),versionPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public ResponseData versionAdd(@RequestBody @Validated(Insert.class)VersionVO vo){
        service.versionAdd(VersionVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData versionUpdate(@RequestBody @Validated(Update.class)VersionVO vo){
        service.versionUpdate(VersionVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData versionUpdate(
            @ApiParam(name = "id", value = "id", required = true, type = "int")
            @RequestParam Integer id){
        service.versionDelete(id);
        return ResponseData.success();
    }

    @GetMapping("getLatestOne")
    @ApiOperation(value = "获取最新一条")
    public ResponseData<VersionDTO> latestOneVersion(){
        Version version = service.getLatestOne();
        return ResponseData.success(VersionDTO.convert(version));
    }

}
